(function($) {
	$.fn.scroller = function(pre_button_id, next_button_id) {
		var $this = $(this);
		var width = $this.width();
		var height = this.height();
		
		var $ul = $this.find('ul');
		var $lis = $ul.find('li');
		var length = $lis.length;
		var this_li = 0;
		var left = 0;
		var this_left = 0;
		
		var pre_button = $('#' + pre_button_id);
		var next_button = $('#' + next_button_id);
		var scroll_id = 0;
		
		var init = function() { 
			$this.css({overflow:'hidden', position:'relative'});
			$ul.css({width:width * (length + 1) + 'px', height:height + 'px', overflow:'hidden', position:'absolute', left:'0px'});
			$lis.css({width:width+'px', height:height + 'px', float:'left', textAlign:'right'});
			
			$lis.eq(0).clone().appendTo($ul);
			
			pre_button.mouseout(function(){
				scroller_forward();
			}).click(function(){
				if (this_li <= 0) {
					left = width * length * (-1);
					this_li = length;
					$ul.css('left', left + 'px');
				}
				clearTimeout(scroll_id);
				to_right();
				return false;
			});
			
			next_button.mouseout(function(){
				scroller_forward();
			}).click(function(){
				clearTimeout(scroll_id);
				to_left();
				return false;
			});
			
			scroll_id = setTimeout(function(){ begin_scroller(); }, 10000);
		}
		
		var begin_scroller = function() {
			to_left();
		}
		var to_left = function() {
			var step = 20;
			if (this_left < width) {
				if (this_left + step > width) {
					step = width - this_left;
				}
				left -= step;
				this_left += step;
				$ul.css('left', left+'px');
				setTimeout(function(){to_left();}, 25);
			} else {
				this_left = 0;
				this_li++;
				if (this_li >= length) {
					left = 0;
					this_li = 0;
					$ul.css('left', '0px');
				}
				scroller_forward();
			}
		}
		var to_right = function() {
			var step = 20;
			if (this_left < width) {
				if (this_left + step > width) {
					step = width - this_left;
				}
				left += step;
				this_left += step;
				$ul.css('left', left+'px');
				setTimeout(function(){to_right();}, 25);
			} else {
				this_left = 0;
				this_li--;
				scroller_forward();
			}
		}
		var scroller_forward = function() {
			clearTimeout(scroll_id);
			scroll_id = setTimeout(function(){ begin_scroller(); }, 10000);
		}
		init();
	}
	
})(jQuery);